package rawengine.physics;

import org.lwjgl.util.vector.*;

public class SimpleMovement extends ODE 
{
  //  gravitational acceleration.
  public final static float G = -9.81f;
  
  protected Vector2f position = new Vector2f();	
  protected Vector2f velocity = new Vector2f();
  
  
  public SimpleMovement(float x0, float y0, 
  							float vx0, float vy0,
                          	float time) {
    //  Call the ODE class constructor
    super(4);

    //  Load the initial position, velocity, and time 
    //  values into the s field and q array from the
    //  ODE class.
    setS(time);
    setQ(vx0,0);
    setQ(x0, 1);
    setQ(vy0,2);
    setQ(y0, 3);
    
  }

  public SimpleMovement(Vector2f position, Vector2f velocity, float time) {
    //  Call the ODE class constructor
    super(4);

    //  Load the initial position, velocity, and time 
    //  values into the s field and q array from the
    //  ODE class.
    //setS(time);
    this.position = position;
    this.velocity = velocity;
    
    setS(time);
    setQ(velocity.x, 0);
    setQ(position.x, 1);
    setQ(velocity.y, 2);
    setQ(position.y, 3);
    
  }	
  public SimpleMovement()
  {
  	super(4);
  }

  //  These methods return the location, velocity, 
  //  and time values
  public void setVx(float vx)
  {
  	setQ(vx, 0);
  }
  
  public void setVy(float vy)
  {
  	setQ(vy, 2);
  }
  
  public void setX(float px)
  {
  	setQ(px, 1);
  }
  
  public void setY(float py)
  {
  	setQ(py, 3);
  }
  
  public float getVx() {
    return getQ(0);
  }

  public float getVy() {
    return getQ(2);
  }

  public float getX() {
    return getQ(1);
  }

  public float getY() {
    return getQ(3);
  }

  public float getTime() {
    return getS();
  }

  //  This method updates the velocity and position
  //  of the projectile according to the gravity-only model.
  public void update(float dt) {
    //  Get current location, velocity, and time values
    //  From the values stored in the ODE class.

   /*
    float time = getS();
    float vx0 = getQ(0);
    float x0 = getQ(1);
    float vy0 = getQ(2);
    float y0 = getQ(3);
    float vz0 = getQ(4);
    float z0 = getQ(5);
   */
    position.x = getQ(1);	
    position.y = getQ(3);
    

    velocity.x = getQ(0);
    velocity.y = getQ(2);
    
	
    //  Update the xyz locations and the z-component
    //  of velocity. The x- and y-velocities don't change.
    /*
    float x = x0 + vx0*dt;
    float y = y0 + vy0*dt;
    float vz = vz0 + G*dt;
    float z = z0 + vz0*dt + 0.5f*G*dt*dt;
    */
    
    position.x = position.x + velocity.x*dt;
    	
    position.y = position.y + velocity.y*dt;	 

    //  Update time;
    //time = time + dt;

    //  Load new values into ODE arrays and fields.
    /*
    setS(time);
    setQ(x, 1);
    setQ(y, 3);
    setQ(vz,4);
    setQ(z, 5);
    */
    setS(dt);	
    setQ(position.x, 1);
    setQ(position.y, 3);
    setQ(velocity.y, 2);
    	

  }

  //  Because SimpleProjectile extends the ODE class,
  //  it must implement the getRightHandSide method.
  //  In this case, the method returns a dummy array.
  public float[] getRightHandSide(float s, float Q[], 
               float deltaQ[], float ds, float qScale) {
    return new float[1];
  }
}
